package roadsuggester.main;

import java.util.ArrayList;
import java.util.List;

import roadsuggester.db.UserTracksDatabase;
import roadsuggester.db.UserLocationsDatabase.UserLocation;
import roadsuggester.tracks.GoogleTrackOverlay;
import roadsuggester.tracks.NavigationTrackOverlay;
import roadsuggester.tracks.RecordedTrack;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.view.Menu;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;


public class TrackerMapOverlay extends Overlay
{
	private Location lastLocation;
	private int lastHeading=0;
	private Drawable arrows[];
	private Context context;
	private UserLocation destination;

	private RecordedTrack recordedTrack;

	private boolean destinationReached=false;

	public TrackerMapOverlay(Context context, UserLocation destination, RecordedTrack track)
	{
		this.context=context;
		this.destination=destination;
		final Resources resources=context.getResources();
		this.recordedTrack=track;

		arrows=new Drawable[] { resources.getDrawable(R.drawable.arrow_0), resources.getDrawable(R.drawable.arrow_5),
				resources.getDrawable(R.drawable.arrow_10), resources.getDrawable(R.drawable.arrow_15), resources.getDrawable(R.drawable.arrow_20),
				resources.getDrawable(R.drawable.arrow_25), resources.getDrawable(R.drawable.arrow_30), resources.getDrawable(R.drawable.arrow_35),
				resources.getDrawable(R.drawable.arrow_40), resources.getDrawable(R.drawable.arrow_45), resources.getDrawable(R.drawable.arrow_50),
				resources.getDrawable(R.drawable.arrow_55), resources.getDrawable(R.drawable.arrow_60), resources.getDrawable(R.drawable.arrow_65),
				resources.getDrawable(R.drawable.arrow_70), resources.getDrawable(R.drawable.arrow_75), resources.getDrawable(R.drawable.arrow_80),
				resources.getDrawable(R.drawable.arrow_85), resources.getDrawable(R.drawable.arrow_90), resources.getDrawable(R.drawable.arrow_95),
				resources.getDrawable(R.drawable.arrow_100), resources.getDrawable(R.drawable.arrow_105),
				resources.getDrawable(R.drawable.arrow_110), resources.getDrawable(R.drawable.arrow_115),
				resources.getDrawable(R.drawable.arrow_120), resources.getDrawable(R.drawable.arrow_125),
				resources.getDrawable(R.drawable.arrow_130), resources.getDrawable(R.drawable.arrow_135),
				resources.getDrawable(R.drawable.arrow_140), resources.getDrawable(R.drawable.arrow_145),
				resources.getDrawable(R.drawable.arrow_150), resources.getDrawable(R.drawable.arrow_155),
				resources.getDrawable(R.drawable.arrow_160), resources.getDrawable(R.drawable.arrow_165),
				resources.getDrawable(R.drawable.arrow_170), resources.getDrawable(R.drawable.arrow_175),
				resources.getDrawable(R.drawable.arrow_180), resources.getDrawable(R.drawable.arrow_185),
				resources.getDrawable(R.drawable.arrow_190), resources.getDrawable(R.drawable.arrow_195),
				resources.getDrawable(R.drawable.arrow_200), resources.getDrawable(R.drawable.arrow_205),
				resources.getDrawable(R.drawable.arrow_210), resources.getDrawable(R.drawable.arrow_215),
				resources.getDrawable(R.drawable.arrow_220), resources.getDrawable(R.drawable.arrow_225),
				resources.getDrawable(R.drawable.arrow_230), resources.getDrawable(R.drawable.arrow_235),
				resources.getDrawable(R.drawable.arrow_240), resources.getDrawable(R.drawable.arrow_245),
				resources.getDrawable(R.drawable.arrow_250), resources.getDrawable(R.drawable.arrow_255),
				resources.getDrawable(R.drawable.arrow_260), resources.getDrawable(R.drawable.arrow_265),
				resources.getDrawable(R.drawable.arrow_270), resources.getDrawable(R.drawable.arrow_275),
				resources.getDrawable(R.drawable.arrow_280), resources.getDrawable(R.drawable.arrow_285),
				resources.getDrawable(R.drawable.arrow_290), resources.getDrawable(R.drawable.arrow_295),
				resources.getDrawable(R.drawable.arrow_300), resources.getDrawable(R.drawable.arrow_305),
				resources.getDrawable(R.drawable.arrow_310), resources.getDrawable(R.drawable.arrow_315),
				resources.getDrawable(R.drawable.arrow_320), resources.getDrawable(R.drawable.arrow_325),
				resources.getDrawable(R.drawable.arrow_330), resources.getDrawable(R.drawable.arrow_335),
				resources.getDrawable(R.drawable.arrow_340), resources.getDrawable(R.drawable.arrow_345),
				resources.getDrawable(R.drawable.arrow_350), resources.getDrawable(R.drawable.arrow_355), };

		int arrowWidth=arrows[lastHeading].getIntrinsicWidth();
		int arrowHeight=arrows[lastHeading].getIntrinsicHeight();
		for(Drawable arrow: arrows)
		{
			arrow.setBounds(0, 0, arrowWidth, arrowHeight);
		}


	}

	public void setLocation(Location newLocation)
	{
		if(destinationReached)
		{
			return;
		}
		recordedTrack.addLocation(newLocation);
		lastLocation=newLocation;

	}


	public boolean setHeading(float heading)
	{
		if(destinationReached)
		{
			return false;
		}
		int newhdg=Math.round(-heading/360*72);
		while(newhdg<0)
			newhdg+=72;
		while(newhdg>71)
			newhdg-=72;

		lastHeading=newhdg;
		return true;
	}


	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
	{
		super.draw(canvas, mapView, shadow);
		final Projection projection=mapView.getProjection();

		drawEndingLocation(canvas, projection);
		drawLocationMarker(canvas, projection);
		recordedTrack.drawTrack(canvas, projection);

		return true;

	}

	private void drawEndingLocation(Canvas canvas, Projection projection)
	{
		if(destination!=null)
		{
			Drawable endMarker=context.getResources().getDrawable(R.drawable.red_dot);
			endMarker.setBounds(0, 0, endMarker.getIntrinsicWidth(), endMarker.getIntrinsicHeight());
			GeoPoint destinationPoint=Utils.coordsToGeoPoint(destination.latitude, destination.longitude);
			Point pt=drawElement(canvas, projection, destinationPoint, endMarker, -endMarker.getIntrinsicWidth()/2, -endMarker.getIntrinsicHeight());
		}

	}

	private void drawLocationMarker(Canvas canvas, Projection projection)
	{
		if(lastLocation==null)
		{
			return;
		}
		int arrowWidth=arrows[lastHeading].getIntrinsicWidth();
		int arrowHeight=arrows[lastHeading].getIntrinsicHeight();
		Point pt=drawElement(canvas, projection, Utils.locationToGeoPoint(lastLocation), arrows[lastHeading], -(arrowWidth/2)+3, -(arrowHeight/2));

		float radius=projection.metersToEquatorPixels(lastLocation.getAccuracy());
		canvas.drawCircle(pt.x, pt.y, radius, Utils.getPaint(R.color.ERROR_RADIUS_BLUE, context));

	}


	private Point drawElement(Canvas canvas, Projection projection, GeoPoint geoPoint, Drawable element, int offsetX, int offsetY)
	{
		Point pt=new Point();
		projection.toPixels(geoPoint, pt);
		canvas.save();
		canvas.translate(pt.x+offsetX, pt.y+offsetY);
		element.draw(canvas);
		canvas.restore();
		return pt;
	}

	public void setDestinationReached(boolean destinationReached)
	{
		this.destinationReached=destinationReached;


	}

	

}
